package string_question.leecode.test43;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/7 10:09
 */
public class Solution2 {

    // 300 栈答案
    public String multiply(String num1, String num2) {
        if (num1.equals("0") || num2.equals("0")) return "0";
        int len1 = num1.length();
        int len2 = num2.length();
        int[] pos = new int[len1 + len2];
        for (int i = len1 - 1; i >= 0; i--) {
            for (int j = len2 - 1; j >= 0; j--) {
                int cur = (num1.charAt(i) - '0') * (num2.charAt(j) - '0');
                int sum = cur + pos[i + j + 1];
                pos[i + j + 1] = sum % 10;
                pos[i + j] += sum / 10;
            }
        }
        StringBuilder ans = new StringBuilder();
        for (int i = 0; i < pos.length; i++) {
            if (i == 0 && pos[i] == 0) {
                continue;
            }
            ans.append(pos[i]);
        }
        return ans.toString();
    }

}
